package com.hsd.znsh.converter;

import com.hsd.znsh.entity.Host;
import com.hsd.znsh.entity.Monitor;
import com.hsd.znsh.enums.MonitorTypeEnum;
import com.hsd.znsh.pojo.output.MonitorVO;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.EnumUtil;

import java.util.ArrayList;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 13:54:52
 */
public class MonitorConverter {

    public static MonitorVO converterToMonitorVO(Monitor monitor, HostRepository hostRepository,
                                                 UserRepository userRepository){
        MonitorVO monitorVO=new MonitorVO();
        BeanUtil.copyProperties(monitor,monitorVO);
        if(monitor.getHostId()!=null){
            Host host=hostRepository.findOne(monitor.getHostId());
            monitorVO.setHostName(host.getHostName());
            monitorVO.setHostSn(host.getHostSn());
        }
        if(monitor.getCreateUserId()!=null){
            monitorVO.setCreateUsername(userRepository.findOne(monitor.getCreateUserId()).getUsername());
        }
        monitorVO.setMonitorTypeName(EnumUtil.getMessageByCode(monitor.getMonitorType(), MonitorTypeEnum.class));
        return monitorVO;
    }

    public static List<MonitorVO> converterToMonitorVOList(List<Monitor> monitors, HostRepository hostRepository,
                                                       UserRepository userRepository){
        List<MonitorVO> list=new ArrayList<>();
        for(Monitor monitor:monitors){
            list.add(converterToMonitorVO(monitor,hostRepository,userRepository));
        }
        return list;
    }
}
